layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 排班主表管理
     */
    var ScheduleInfo = {
        tableId: "scheduleInfoTable"
    };

    /**
     * 初始化表格的列
     */
    ScheduleInfo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {align: 'center', toolbar: '#tableBar', title: '操作'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'storeName', sort: true, title: '门店'},
            {field: 'plantDate', sort: true, title: '排班日期'},
            {field: 'week', sort: true, title: '周几'},
            {field: 'weather', sort: true, title: '天气'},
            {field: 'samePeriodBusiness', sort: true, title: '同期营业'},
            {field: 'tableMaker', sort: true, title: '制表人'},
            {field: 'estimatedTurnover', sort: true, title: '预估营业额'},
            {field: 'actualRevenue', sort: true, title: '实际营业额'},
            {field: 'estimatedWorkingHours', sort: true, title: '预估工时'},
            {field: 'actualHour', sort: true, title: '实际工时'},
            {field: 'createTime', sort: true, hide: true,title: '创建时间'},
            {field: 'createUser', sort: true, hide: true,title: '创建人'},
            {field: 'updateTime', sort: true, hide: true,title: '更新时间'},
            {field: 'updateUser', sort: true, hide: true,title: '更新人'},

        ]];
    };

    /**
     * 点击查询按钮
     */
    ScheduleInfo.search = function () {
        var queryData = {};
        if($("#plant_date").val() != ""){
            queryData['plantDate'] = $("#plant_date").val();
        }
        queryData['storeName'] = $("#store_name").val();
        table.reload(ScheduleInfo.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    ScheduleInfo.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/scheduleInfo/add';
    };

    /**
     * 弹出排班对话框
     */
    ScheduleInfo.openPlantDlg = function () {
        window.location.href = Feng.ctxPath + '/scheduleInfo/plant';
    };

    /**
     * 弹出排班对话框
     */
    ScheduleInfo.openUpdatePlantDlg = function () {
        window.location.href = Feng.ctxPath + '/scheduleInfo/updatePlant';
    };

    /**
     * 导出excel按钮
     */
    ScheduleInfo.exportExcel = function () {
        var checkRows = table.checkStatus(ScheduleInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ScheduleInfo.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/scheduleInfo/edit?id=' + data.id;
    };

    /**
     * 点击排班详情
     *
     *
     * @param data 点击按钮时候的行数据
     */
    ScheduleInfo.onDetailItem = function (data) {
        window.location.href = Feng.ctxPath + '/scheduleInfo/scheduleInfo_detail?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ScheduleInfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/scheduleInfo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ScheduleInfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ScheduleInfo.tableId,
        url: Feng.ctxPath + '/scheduleInfo/list',
        page: false,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: ScheduleInfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ScheduleInfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ScheduleInfo.openAddDlg();
    });

    // 排班按钮点击事件
    $('#btnPlant').click(function () {
        ScheduleInfo.openPlantDlg();
    });
    // 修改按钮点击事件
    $('#btnUpdatePlant').click(function () {
        ScheduleInfo.openUpdatePlantDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ScheduleInfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ScheduleInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ScheduleInfo.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ScheduleInfo.onDeleteItem(data);
        } else if (layEvent === 'detail') {
            ScheduleInfo.onDetailItem(data);
        }
    });
});
